package com.bytecodes.eaulas.mantenimiento;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.rmi.RemoteException;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import com.bytecodes.eaulas.beans.Recurso;
import com.bytecodes.eaulas.comun.EAulasTableModel;
import com.bytecodes.eaulas.comun.Utils;
import com.bytecodes.eaulas.comun.servicios.EAulasServiciosImpl;
import com.bytecodes.eaulas.comun.servicios.EAulasServiciosInterface;
import com.bytecodes.eaulas.comun.servicios.TDSLanguageUtils;



/**
 * @author David Fern�ndez G�mez
 */
public class PanelBajaRecurso extends JPanel {
	
	private EAulasServiciosInterface servicios;
	private String idCliente;
	public PanelBajaRecurso(String idCliente) {
		this.idCliente= idCliente;
		try {
			servicios = new EAulasServiciosImpl();
		} catch (RemoteException exception) {
			exception.printStackTrace();
		}
		initComponents();
	}

	public JTable getTableResultados() {
		return tableResultados;
	}

	private void buttonEliminarActionPerformed(ActionEvent e) {
		Recurso recurso= new Recurso();
		
		if(!"".equals(this.idRecurso)){
			recurso.setId(new Integer(this.idRecurso));
			try {
				int result = JOptionPane.showConfirmDialog(null, TDSLanguageUtils.getMessage("panelGestionRecurso.borrar.pregunta"),
						 TDSLanguageUtils.getMessage("panelGestionRecurso.borrar"), JOptionPane.OK_CANCEL_OPTION);
				if(result==0){
					servicios.borraRecurso(recurso, this.idCliente);
					JOptionPane.showMessageDialog(null, TDSLanguageUtils
							.getMessage("panelGestionRecurso.borrar.ok"),
							TDSLanguageUtils.getMessage("panelGestionRecurso.borrar"),
							JOptionPane.INFORMATION_MESSAGE);
					Utils.clearPanel(this);
					tableModel= servicios.getTableModel("all_resources", " where inactivation_date is null");
					tableModel.fireTableDataChanged();
					tableResultados.setModel(tableModel);
				}
			} catch (RemoteException exception) {
				exception.printStackTrace();
			}
		}
		else{
			JOptionPane.showMessageDialog(null, TDSLanguageUtils
					.getMessage("panelGestionRecurso.borrar.ko"),
					TDSLanguageUtils.getMessage("panelGestionRecurso.borrar"),
					JOptionPane.ERROR_MESSAGE);
		}
	}

	private void buttonCancelActionPerformed(ActionEvent e) {
		// TODO add your code here
	}

	private boolean rellenaObjeto(Recurso objetoRecurso) {
		try{
			objetoRecurso.setDescripcion(this.textFieldDescipcion.getText());
			objetoRecurso.setCantidad(new Integer (this.textFieldCantidad.getText()));
			objetoRecurso.setPrecioDia(new Double (this.textFieldPrecioDia.getText()));
			objetoRecurso.setPrecioHora(new Double (this.textFieldPrecioHora.getText()));
		}catch (NumberFormatException e) {
			Utils.mostraMensajeError(TDSLanguageUtils.getMessage("panelGestionRecurso.datos.numericos"), TDSLanguageUtils.getMessage("panelGestionRecurso.anadir"));
			return false;
		}
		return true;
	}
	
	private void buttonBuscarActionPerformed(ActionEvent e) {
		String descripcion =this.textFieldDescipcion.getText();
		try {
			String filtro = "";
			if(!"".equals(descripcion)){
				 filtro = " WHERE resourcedescription like '%"+descripcion+"%'"; 

			}
	
			 tableModel= servicios.getTableModel("all_resources", filtro);
			 tableModel.fireTableDataChanged();
			 tableResultados.setModel(tableModel);
		} catch (RemoteException exception) {
			System.out.println("Error buscando");
			System.out.println(exception.getMessage());
		}
	}


	private void initComponents() {
		label1 = new JLabel();
		label2 = new JLabel();
		textFieldDescipcion = new JTextField();
		buttonEliminar = new JButton();
		buttonCancel = new JButton();
		buttonBuscar= new JButton();
		comboAulas = new JComboBox();
		comboAulas.addItem("");
		label3 = new JLabel();
		textFieldCantidad = new JTextField();
		label4 = new JLabel();
		textFieldPrecioHora = new JTextField();
		label5 = new JLabel();
		textFieldPrecioDia = new JTextField();
		hashAula = new Hashtable<String, Integer>();
		scrollPane1 = new JScrollPane();
		
		//---- label1 ----
		label1.setText(TDSLanguageUtils.getMessage("panelGestionAula.aula"));

		//---- label2 ----
		label2.setText(TDSLanguageUtils.getMessage("panelGestionRecurso.descripcion"));

		//---- label3 ----
		label3.setText(TDSLanguageUtils.getMessage("panelGestionRecurso.cantidad"));

		//---- label4 ----
		label4.setText(TDSLanguageUtils.getMessage("panelGestionRecurso.precioHora"));

		//---- label5 ----
		label5.setText(TDSLanguageUtils.getMessage("panelGestionRecurso.precioDia"));

		//---- buttonEliminar ----
		buttonEliminar.setText(TDSLanguageUtils.getMessage("panelGestionRecurso.eliminar"));
		buttonEliminar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				buttonEliminarActionPerformed(e);
			}
		});

		//---- buttonCancel ----
		buttonCancel.setText(TDSLanguageUtils.getMessage("panelGestionRecurso.cancelar"));
		buttonCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				buttonCancelActionPerformed(e);
			}
		});

		//---- buttonBuscar ----
		buttonBuscar.setText(TDSLanguageUtils.getMessage("panelGestionRecurso.buscar"));
		buttonBuscar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				buttonBuscarActionPerformed(e);
			}
		});
		
		

		try {
			tableModel= servicios.getTableModel("all_resources", " where inactivation_date is null");
		} catch (RemoteException exception1) {
			exception1.printStackTrace();
		}
		tableResultados = new JTable(tableModel);
		tableResultados.addMouseListener(new MouseAdapter() 
		   {
		      public void mouseClicked(MouseEvent e) 
		      {
		         int fila = tableResultados.rowAtPoint(e.getPoint());
		        // int columna = tableResultados.columnAtPoint(e.getPoint());
		         rellenarDatos( fila );
		      }

			
		   });
		
		GroupLayout layout = new GroupLayout(this);
		setLayout(layout);
		layout.setHorizontalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addGap(165, 165, 165)
					.addComponent(buttonBuscar)
					.addGap(38, 38, 38)
					.addComponent(buttonEliminar)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
					.addComponent(buttonCancel)
					.addGap(253, 253, 253))
				.addGroup(layout.createSequentialGroup()
					.addGap(23, 23, 23)
					.addGroup(layout.createParallelGroup()
						.addGroup(layout.createSequentialGroup()
							.addComponent(label1)
							.addGap(76, 76, 76)
							.addComponent(comboAulas, GroupLayout.PREFERRED_SIZE, 261, GroupLayout.PREFERRED_SIZE)
							.addContainerGap())
						.addGroup(layout.createSequentialGroup()
							.addGroup(layout.createParallelGroup()
								.addGroup(layout.createSequentialGroup()
									.addComponent(label4)
									.addGap(43, 43, 43)
									.addComponent(textFieldPrecioHora, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE))
								.addGroup(layout.createSequentialGroup()
									.addGroup(layout.createParallelGroup()
										.addComponent(label3)
										.addComponent(label2))
									.addGap(43, 43, 43)
									.addGroup(layout.createParallelGroup()
										.addComponent(textFieldDescipcion, GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
										.addComponent(textFieldCantidad, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)))
								.addGroup(layout.createSequentialGroup()
									.addComponent(label5)
									.addGap(51, 51, 51)
									.addGroup(layout.createParallelGroup()
										.addComponent(tableResultados, GroupLayout.PREFERRED_SIZE, 402, GroupLayout.PREFERRED_SIZE)
										.addComponent(textFieldPrecioDia, GroupLayout.PREFERRED_SIZE, 141, GroupLayout.PREFERRED_SIZE))))
							.addGap(198, 198, 198))))
		);
		layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {buttonBuscar, buttonCancel, buttonEliminar});
		layout.setVerticalGroup(
			layout.createParallelGroup()
				.addGroup(layout.createSequentialGroup()
					.addGap(23, 23, 23)
					.addGroup(layout.createParallelGroup()
						.addComponent(label1)
						.addComponent(comboAulas, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18, 18, 18)
					.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
						.addComponent(label2)
						.addComponent(textFieldDescipcion, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(34, 34, 34)
					.addGroup(layout.createParallelGroup()
						.addGroup(layout.createSequentialGroup()
							.addGap(3, 3, 3)
							.addComponent(label3))
						.addComponent(textFieldCantidad, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18, 18, 18)
					.addGroup(layout.createParallelGroup()
						.addGroup(layout.createSequentialGroup()
							.addGap(3, 3, 3)
							.addComponent(label4))
						.addComponent(textFieldPrecioHora, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(24, 24, 24)
					.addGroup(layout.createParallelGroup()
						.addGroup(layout.createSequentialGroup()
							.addGap(3, 3, 3)
							.addComponent(label5))
						.addComponent(textFieldPrecioDia, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
					.addComponent(tableResultados, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE)
					.addGap(33, 33, 33)
					.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
						.addComponent(buttonBuscar)
						.addComponent(buttonCancel)
						.addComponent(buttonEliminar))
					.addGap(32, 32, 32))
		);
		layout.linkSize(SwingConstants.VERTICAL, new Component[] {buttonCancel, buttonEliminar});
		
		tableResultados.getColumnModel().getColumn(0).setPreferredWidth(0);
		tableResultados.getColumnModel().getColumn(0).setMaxWidth(0);
		tableResultados.getColumnModel().getColumn(5).setPreferredWidth(0);
		tableResultados.getColumnModel().getColumn(5).setMaxWidth(0);
		tableResultados.getColumnModel().getColumn(6).setPreferredWidth(0);
		tableResultados.getColumnModel().getColumn(6).setMaxWidth(0);
		tableResultados.getColumnModel().getColumn(7).setPreferredWidth(0);
		tableResultados.getColumnModel().getColumn(7).setMaxWidth(0);
		tableResultados.getColumnModel().getColumn(8).setPreferredWidth(0);
		tableResultados.getColumnModel().getColumn(8).setMaxWidth(0);
		tableResultados.getColumnModel().getColumn(9).setPreferredWidth(0);
		tableResultados.getColumnModel().getColumn(9).setMaxWidth(0);
		tableResultados.getColumnModel().removeColumn(new TableColumn(0));
		
		this.label1.setVisible(false);
		this.comboAulas.setVisible(false);
	}
	
	
	 	protected void rellenarDatos(int fila) {
	 		//Utils.clearPanel(this);
			if(tableModel.getValueAt(fila,1)!=null)
				this.textFieldDescipcion.setText(tableModel.getValueAt(fila,1).toString());
			if(tableModel.getValueAt(fila,2)!=null)
				this.textFieldCantidad.setText(tableModel.getValueAt(fila,2).toString());
			if(tableModel.getValueAt(fila,3)!=null)
				this.textFieldPrecioHora.setText(tableModel.getValueAt(fila,3).toString());
			if(tableModel.getValueAt(fila,4)!=null)
				this.textFieldPrecioDia.setText(tableModel.getValueAt(fila,3).toString());
			
			this.idRecurso=tableModel.getValueAt(fila,0).toString();
	}


		private JLabel label2;
		private JTextField textFieldDescipcion;
		private JButton buttonEliminar;
		private JButton buttonCancel;
		private JTable tableResultados;
		private JButton buttonBuscar;
		private JLabel label3;
		private JTextField textFieldCantidad;
		private JTextField textFieldPrecioHora;
		private JLabel label4;
		private JLabel label5;
		private JTextField textFieldPrecioDia;
		private JLabel label1;
		private JComboBox comboAulas;
		private Hashtable<String, Integer> hashAula;
		private EAulasTableModel tableModel;
		private JScrollPane scrollPane1 ;
		private String idRecurso="";
}
